//
//  MXHomeDetialView.m
//  CatchBear
//
//  Created by 王启颖 on 2018/11/23.
//  Copyright © 2018年 王启颖. All rights reserved.
//

#import "MXHomeDetialView.h"
#import "MXHomeDetialCell.h"
#import "MXHomeDetialFootView.h"
#import "MXCommentsController.h"
#import "MXHomeDetialCommentsView.h"
#import "MXCommentsModel.h"

@interface MXHomeDetialView () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *paramsArr;
@property (strong, nonatomic) NSMutableArray *commentParamsArr;

@property (strong, nonatomic) NSMutableDictionary *dicParams;

@end

@implementation MXHomeDetialView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc]initWithFrame:self.bounds];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = MX_WHITE_COLOR;
        self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        [self addSubview:self.tableView];
    }
    return self;
}

-(void)setModel:(MXHomePageModel *)model{
    _model = model;

    [self getMessageCommentsRequest];
    
}

//初始化页面
- (void)initCustomerControls {
    [self.paramsArr removeAllObjects];
    for (id pic in _model.picturesList) {
        Picture *picture = [Picture mj_objectWithKeyValues:pic];
        [self.paramsArr addObject:picture];
    }
    
    //图片以下位置
    MXHomeDetialFootView *footView = [MXHomeDetialFootView initMXHomeDetialFootView];
    footView.navigationController = self.navigationController;
    CGFloat lab_height = [_model.messages_info caculateTextHeightWithSize:15];
    CGFloat comment_height = [self caculateHomeDetialCommentsViewHeight];
    footView.frame = CGRectMake(0,0, kScreenWidth, 150.f + lab_height + comment_height);
    footView.model = self.model;
    __weak typeof(self) weakself = self;
    footView.agreesBlock = ^(MXHomePageModel *model) {
        weakself.homeDetialBlock(model);
    };
    footView.commentsBlock = ^(MXHomePageModel *model) {
        MXCommentsController *commentsController = [[MXCommentsController alloc]init];
        commentsController.commentsBlock = ^(MXHomePageModel *model) {
            weakself.model = model;
            weakself.homeDetialBlock(model);
        };
        [weakself.navigationController pushViewController:commentsController animated:YES];
        commentsController.model  = model;
    };
    
    //评论区
    MXHomeDetialCommentsView *hdCommentView = [[MXHomeDetialCommentsView alloc]initWithFrame:CGRectMake(0, 150.f + lab_height, kScreenWidth, comment_height)];
    hdCommentView.commentParamsArr = self.commentParamsArr;
    hdCommentView.messages_id = self.model.messages_id;
    hdCommentView.commentsBlock = ^{
        weakself.model.messages_commentnum = weakself.model.messages_commentnum + 1;
        [weakself getMessageCommentsRequest];
    };

    hdCommentView.commentsTableBlock = ^{
        MXCommentsController *commentsController = [[MXCommentsController alloc]init];
        commentsController.commentsBlock = ^(MXHomePageModel *model) {
            weakself.model = model;
            weakself.homeDetialBlock(model);
        };
        [weakself.navigationController pushViewController:commentsController animated:YES];
        commentsController.model  = weakself.model;
    };
    [footView addSubview:hdCommentView];
    
    self.tableView.tableFooterView = footView;
    [self.tableView reloadData];
}

-(CGFloat)caculateHomeDetialCommentsViewHeight{
    CGFloat tableHeight = 44;
    for (id res in self.commentParamsArr) {
        MXCommentsModel *comModel = [MXCommentsModel mj_objectWithKeyValues:res];
        NSString *comments_info = [NSString stringWithFormat:@"%@:%@",comModel.user.user_nickname,comModel.comments_info];
        CGFloat labHeight = [comments_info caculateTextHeightWithSize:15];
        tableHeight = tableHeight + labHeight;
    }
    
    return tableHeight;
}

#pragma mark - UITableViewDelegate -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.paramsArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MXHomeDetialCell *cell = [MXHomeDetialCell cellWithTableView:tableView];

    Picture *pic = self.paramsArr[indexPath.row];
    
    [cell.homeDetial_image sd_setImageWithURL:[NSURL URLWithString:pic.picture_url] placeholderImage:[UIImage imageNamed:@"photoAndVideoDefault"]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Picture *pic = self.paramsArr[indexPath.row];
    return [pic.picture_size caculateToPicHeight];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}



#pragma mark - 懒加载 -
-(NSMutableArray *)paramsArr{
    if (!_paramsArr) {
        _paramsArr = [[NSMutableArray alloc]init];
    }
    return _paramsArr;
}

-(NSMutableArray *)commentParamsArr{
    if (!_commentParamsArr) {
        _commentParamsArr = [[NSMutableArray alloc]init];
    }
    return _commentParamsArr;
}

- (NSMutableDictionary *)dicParams {
    if (!_dicParams) {
        _dicParams = [NSMutableDictionary dictionary];
    }
    [self initdicParams];
    return _dicParams;
}
- (void)initdicParams {
    _dicParams[@"messages_id"] = self.model.messages_id;
    _dicParams[@"pageNum"] = @"1";
    _dicParams[@"pageSize"] = @"3";
}
#pragma mark - 网络请求 -
-(void)getMessageCommentsRequest{
    [SVProgressHUD show];
    
    [[MXHttpManagerClass HttpRequstManager] PostURL:getMessageComments params:self.dicParams isHearderparam:YES success:^(NSInteger code, id result) {
        [SVProgressHUD dismiss];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        if (code == 0) {
            
            NSLog(@"%@",result);
            [self.commentParamsArr removeAllObjects];
            
            for (id res in result[@"message"]) {
                [self.commentParamsArr addObject:res];
            }
            [self initCustomerControls];
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"参数错误"];
        }
        
    } fail:^(NSError *error) {
        [SVProgressHUD dismiss];
        NSLog(@"%@",error);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
}




@end
